package com.aaa.system.service.impl;

import com.aaa.entity.CheckItem;
import com.aaa.entity.User;
import com.aaa.system.dao.CheckItemDao;
import com.aaa.system.dao.UserDao;
import com.aaa.system.service.ICheckItemService;
import com.aaa.system.vo.CheckItemVo;
import com.aaa.utils.JWTUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 检查费用表 服务实现类
 * </p>
 *
 * @author L
 * @since 2023-03-24
 */
@Service
public class CheckItemServiceImpl extends ServiceImpl<CheckItemDao, CheckItem> implements ICheckItemService {

    @Resource
    private CheckItemDao checkItemDao;

    @Resource
    private UserDao userDao;

    @Override
    public Result<IPage<CheckItem>> selectAll(Integer current, Integer size, CheckItem checkItem) {
        IPage<CheckItem> page = new Page<>(current,size);
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(checkItem.getCheckItemName())){
            queryWrapper.like("check_item_name",checkItem.getCheckItemName());
        }
        if (StringUtils.hasText(checkItem.getKeywords())){
            queryWrapper.like("keywords",checkItem.getKeywords());
        }
        if (Objects.nonNull(checkItem.getTypeId())){
            queryWrapper.eq("type_id",checkItem.getTypeId());
        }
        if (Objects.nonNull(checkItem.getStatus())){
            queryWrapper.eq("status",checkItem.getStatus());
        }
        IPage<CheckItem> checkItemIPage = checkItemDao.selectPage(page,queryWrapper);
        return new Result<>(200,"成功查询所有检查费用设置",checkItemIPage);
    }

    @Override
    public Result<Boolean> saveOrUpdateInsItem(CheckItem checkItem) {
        //直接在token获取手机号
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> tokenInfo = JWTUtils.getInfo(token);
        String phone = tokenInfo.get("username").toString();
        //通过token获取登录用户信息
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User userByToken = userDao.selectOne(queryWrapper);


        if (Objects.nonNull(checkItem.getCheckItemId())){
            //如果有检查项目id就是执行修改操作
            checkItem.setUpdateTime(LocalDateTime.now());
            checkItem.setUpdateBy(userByToken.getUserName());
            int i = checkItemDao.updateById(checkItem);
            return new Result<>(200,"修改检查项目成功",i>0?true:false);
        }else {
            //如果没有检查项目id就是执行添加操作
            checkItem.setCreateTime(LocalDateTime.now());
            checkItem.setCreateBy(userByToken.getUserName());
            int insert = checkItemDao.insert(checkItem);
            return new Result<>(200,"添加检查项目成功",insert>0?true:false);
        }
    }

    @Override
    public Result<Boolean> deleteCheckItemByIds(Long[] checkItemIds) {
        Boolean b = null;
        if (checkItemIds.length>0){
             b = checkItemDao.batchCheckItemById(checkItemIds);
        }
        return new Result<>(200,"删除成功",b);
    }

    @Override
    public List<CheckItem> getChickItemNameList() {
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        List<CheckItem> checkItems = checkItemDao.selectList(queryWrapper);
        return checkItems;
    }

    @Override
    public Result<List<CheckItem>> findAll() {
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        List<CheckItem> checkitem = checkItemDao.selectList(queryWrapper);
        return new Result<>(200,"查询所有部门",checkitem);
    }

    @Override
    public Result<IPage<CheckItem>> chajk(Integer currentPage, Integer pageSize, CheckItemVo checkItemVo) {
        IPage<CheckItem> page = new Page<>(currentPage,pageSize);

        QueryWrapper<CheckItem> wappers = new QueryWrapper<>();
        if (StringUtils.hasText(checkItemVo.getKeywords())){
            wappers.like("keywords",checkItemVo.getKeywords());
        }
        IPage<CheckItem> page1 = checkItemDao.selectPage(page, wappers);

        return new Result<>(200,"查询部门接口",page1);
    }
}
